// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   KeyGen.java

package com.vandagroup.common.util.seqgener;

import com.vandagroup.common.bean.BaseBean;
import com.vandagroup.common.po.ESSPL;
import com.vandagroup.common.po.PortalPO;
import com.vandagroup.common.util.DataFormat;
import java.io.PrintStream;
import java.sql.SQLException;

// Referenced classes of package com.vandagroup.common.util.seqgener:
//            SysSeqPO, SysSeqFormBean

public class KeyGen
{

    public KeyGen()
    {
    }

    public static synchronized String getKey(String seqObj, int length)
        throws SQLException
    {
        return DataFormat.intToString(getKey(seqObj), length);
    }

    public static synchronized String getKey(String seqObj, int length, int startNO)
        throws SQLException
    {
        return DataFormat.intToString(startNO + getKey(seqObj), length);
    }

    public static synchronized String getKey(String seqObj, int length, int startNO, int endNO)
        throws Exception
    {
        int tempNO = startNO + getKey(seqObj);
        if(tempNO <= endNO)
            return DataFormat.intToString(tempNO, length);
        else
            throw new Exception("out");
    }

    public static synchronized String getUnitOperKey(String custID)
        throws Exception
    {
        String tempNO = null;
        try
        {
            tempNO = getKey(custID, 8, Integer.parseInt(custID), Integer.parseInt(custID) + 9);
        }
        catch(Exception e)
        {
            if(e.getMessage().equals("out"))
                throw new Exception("xxx" + custID + "xxxxxxx");
        }
        return tempNO;
    }

    public static synchronized int getKey(String seqObj)
        throws SQLException
    {
    	// TODO chage to get key from sequence
        SysSeqPO po = new SysSeqPO();
        po.setWhereClause("SEQOBJ = '" + seqObj.toUpperCase() + "'");
        SysSeqFormBean bean = (SysSeqFormBean)po.retrieveForm();
        int iRtnSeq = 1;
        if(bean == null)
        {
            bean = new SysSeqFormBean();
            bean.setSeqObj(seqObj.toUpperCase());
            bean.setSeqNO(2);
            po.setSysSeqForm(bean);
            int iRow = po.add();
            iRtnSeq = 1;
        } else
        {
            iRtnSeq = bean.getSeqNO();
            int i = updateSeq(seqObj, iRtnSeq);
        }
        return iRtnSeq;
    }

    public static synchronized int getKey(String seqObj, String dbname)
        throws SQLException
    {
        SysSeqPO po = new SysSeqPO();
        po.setDbName(dbname);
        po.setWhereClause("SEQOBJ = '" + seqObj.toUpperCase() + "'");
        SysSeqFormBean bean = (SysSeqFormBean)po.retrieveForm();
        int iRtnSeq = 1;
        if(bean == null)
        {
            bean = new SysSeqFormBean();
            bean.setSeqObj(seqObj.toUpperCase());
            bean.setSeqNO(2);
            po.setSysSeqForm(bean);
            int iRow = po.add();
            iRtnSeq = 1;
        } else
        {
            iRtnSeq = bean.getSeqNO();
            int i = updateSeq(seqObj, iRtnSeq, dbname);
        }
        return iRtnSeq;
    }

    public static String getOperKey(String tag)
        throws Exception
    {
        if(tag.equals("bank"))
        {
            int tempKey = getKey("bankOper");
            if(tempKey > 0x98967f)
                throw new Exception("���в���Ա�ų���");
            else
                return DataFormat.intToString(tempKey, 8);
        }
        if(tag.equals("group"))
        {
            int tempKey = (0xf4240 + getKey("groupOper")) - 1;
            if(tempKey > 0x2dc6bf)
                throw new Exception("���Ų���Ա�ų���");
            else
                return DataFormat.intToString(tempKey, 7) + "0";
        }
        if(tag.equals("crop"))
        {
            int tempKey = (0x2dc6c0 + getKey("cropOper")) - 1;
            if(tempKey > 0x5b8d7f)
                throw new Exception("��ҵ����Ա�ų���");
            else
                return DataFormat.intToString(tempKey, 7) + "0";
        }
        if(tag.equals("priv"))
        {
            int tempKey = (0x3938700 + getKey("privOper")) - 1;
            if(tempKey > 0x5f5e0ff)
                throw new Exception("��ҵ����Ա�ų���");
            else
                return DataFormat.intToString(tempKey, 8);
        } else
        {
            return null;
        }
    }

    public static String getKeyType(String key)
    {
        try
        {
            int tempKey = Integer.parseInt(key);
            if(tempKey > 0 && tempKey <= 0x98967f)
                return "bank";
            if(tempKey > 0x989680 && tempKey <= 0x1c9c37f)
                return "group";
            if(tempKey > 0x1c9c380 && tempKey <= 0x39386ff)
                return "crop";
            if(tempKey > 0x3938700 && tempKey <= 0x5f5e0ff)
                return "priv";
        }
        catch(Exception e)
        {
            return "";
        }
        return null;
    }

    public static boolean isCrop(String key)
    {
        return getKeyType(key).equals("group") || getKeyType(key).equals("crop");
    }

    public static int updateSeq(String seqObj, int seqNO)
        throws SQLException
    {
        SysSeqPO po = new SysSeqPO();
        SysSeqFormBean formbean = new SysSeqFormBean();
        formbean.setSeqObj(seqObj.toUpperCase());
        formbean.setSeqNO(seqNO + 1);
        po.setSysSeqForm(formbean);
        po.setWhereClause("SEQOBJ = '" + seqObj.toUpperCase() + "'");
        po.update();
        return 0;
    }

    public static int updateSeq(String seqObj, int seqNO, String dbname)
        throws SQLException
    {
        SysSeqPO po = new SysSeqPO();
        po.setDbName(dbname);
        SysSeqFormBean formbean = new SysSeqFormBean();
        formbean.setSeqObj(seqObj.toUpperCase());
        formbean.setSeqNO(seqNO + 1);
        po.setSysSeqForm(formbean);
        po.setWhereClause("SEQOBJ = '" + seqObj.toUpperCase() + "'");
        po.update();
        return 0;
    }

    public static synchronized int getUnionKey(String tableName, String keyField, String fkName, String fkValue, boolean isInt)
        throws SQLException
    {
        String deli = isInt ? "" : "'";
        String sql = "SELECT max(" + keyField + ") FROM " + tableName + " WHERE " + fkName + "=" + deli + fkValue + deli;
        ESSPL pl = new ESSPL();
        Object obj = pl.retrieve(sql);
        if(obj == null)
            return 1;
        if(isInt)
            return ((Integer)obj).intValue() + 1;
        else
            return Integer.parseInt(String.valueOf(obj)) + 1;
    }

    public static synchronized String getKey(String tableName, String keyName, int len)
        throws Exception
    {
        String key = "";
        PortalPO po = new PortalPO();
        try
        {
            String sql = "SELECT MAX( " + keyName + " ) KEY FROM " + tableName;
            BaseBean bean = po.queryBean(sql);
            if(bean.get("KEY") == null || bean.get("KEY") == "")
                key = DataFormat.intToString(1, len);
            else
                key = DataFormat.intToString(Integer.parseInt((String)bean.get("KEY")) + 1, len);
        }
        catch(Throwable t)
        {
            System.out.println("ȡ" + tableName + "���е����кţ�" + keyName + "����?");
            t.printStackTrace();
        }
        return key;
    }
}
